<?php 
// +----------------------------------------------------------------------
// | Item
// +----------------------------------------------------------------------
// | Copyright
// +----------------------------------------------------------------------
// | Versions
// +----------------------------------------------------------------------
// | Author       YanHao
// +----------------------------------------------------------------------
// | Date         
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Base as controller;
use think\Response\Redirect;
use think\Response;
use think\Session;
use think\Config;
use com\Auth;

class Base extends controller
{
    protected $uid; //管理员ID

    protected $header = '主页'; //页头
    protected $desc   = '首页'; //页脚
    protected $model = null; //模型

    protected $module = 'admin'; //对应模块
    protected $lineStatus;
    protected static $_session_uinfo = 'a_uinfo'; 
                                 
    /**
     * 初始
     * @Author  YanHao
     * @Date    2017-07-04
     * @version [version]
     * @return  [type]     [description]
     */
    public function _initialize()
    {
        Session::init();
        $this->_checkLogin(); // 登录检查
        $this->view->config('tpl_cache', false); // 关闭模板缓存
        if ($this->request->isPjax()) {
            Config::set('default_ajax_return', 'html');
            $this->view->engine->layout(false);
        }
        $this->view->header = $this->header;
        $this->view->desc   = $this->desc;

        $this->initialize();
    }
    //初始化
    public function initialize(){}

    /**
     * 登录检查
     * @Author  YanHao
     * @Date    2017-07-04
     * @version [version]
     * @return  [type]     [description]
     */
    private function _checkLogin()
    {
        $uid = Session::get(Config::get('session_name.uid'));
        if ($uid) {
            $this->uid = $uid;
            $this->lineStatus = Session::get(Config::get('session_name.a_uinfo'))['line_status']; //是否在线
        } else {
            $response = new Redirect('Login/index');
            $response->code(302);
            throw new \think\exception\HttpResponseException($response);
        }
    }
    /**
     * 刷新管理员session
     * @Author  YanHao
     * @Date    2017-07-05
     * @version [version]
     * @return  [type]     [description]
     */
    public function refreshSession()
    {
        $info = parent::model('Admin')->getOne($this->uid, true);
        return Session::set(self::$_session_uinfo, $info);
    }


    ///////////////////////////////////
    ///// 离线禁止任何操作 待完善 /////
    ///////////////////////////////////
    /**
     * 检查是否在线 不在线不可以操作跳转到登录页面
     * @Author  YanHao
     * @Date    2017-07-06
     * @version [version]
     * @return  [type]     [description]
     */
    private function _checkStatus()
    {
        return $this->lineStatus ? true : ['code' => 0, 'msg' => '您处于离线状态,请上线后操作!'];
    }

}